define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'choi/language_question/index' + location.search,
                    add_url: 'choi/language_question/add',
                    edit_url: 'choi/language_question/edit',
                    del_url: 'choi/language_question/del',
                    multi_url: 'choi/language_question/multi',
                    table: 'language_question',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'language_id',
                sortName: 'weigh',
                search: false,
                commonSearch: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'language_id', title: __('Language_id')},
                        {field: 'question_id', title: __('Question_id')},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        lists: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'choi/language_question/lists' + location.search,
                    add_url: 'choi/language_question/add',
                    edit_url: 'choi/language_question/edit',
                    del_url: 'choi/language_question/del',
                    multi_url: 'choi/language_question/multi',
                    create_url: 'choi/language_question/create',
                    table: 'language_question',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // pk: 'language_question_id',
                sortName: 'weigh',
                search: false,
                commonSearch: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                pagination: false,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'language_id', title: __('Language_id')},
                        {field: 'question_id', title: __('Question_id')},
                        {field: 'question.title', title: __('题目')},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                                return ` <a href="javascript:;" class="btn btn-xs btn-success btn-edit" data-language_id="`+data.language_id+`" data-question_id="`+data.question_id+`" title="编辑" ><i class="fa fa-pencil"></i></a> ` +
                                    ` <a href="javascript:;" class="btn btn-xs btn-danger btn-delete" data-language_id="`+data.language_id+`" data-question_id="`+data.question_id+`" title="删除" ><i class="fa fa-trash"></i></a> `;

                            }}
                    ]
                ]
            });

            //添加
            $('#toolbar').on('click', '.create', function () {
                var id = $(this).data('id');
                Fast.api.open('/keyu.php/choi/language_question/create?ids='+id, '添加题目');
            });

            //编辑
            table.on('click', '.btn-edit', function () {
                var languageId = $(this).data('language_id');
                var questionId = $(this).data('question_id');
                Fast.api.open('/keyu.php/choi/language_question/edit?language_id='+languageId+'&question_id='+questionId, '编辑');
            });

            //删除
            table.on('click', '.btn-delete', function () {
                var languageId = $(this).data('language_id');
                var questionId = $(this).data('question_id');
                layer.confirm('您确定要删除吗?', {icon: 3, title:'提示'}, function(index){
                    $.get('/keyu.php/choi/language_question/delete?language_id='+languageId+'&question_id='+questionId, function (res) {
                        if (res) {
                            layer.msg('删除成功');
                            setTimeout(function () {
                                window.location.reload();
                            }, 1000)
                        } else {
                            layer.msg('删除失败，请联系管理员');
                        }
                    })
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        create: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});